package cn.rmc.basic.service.impl;

import cn.rmc.basic.mapper.BaseMapper;

import cn.rmc.basic.query.BaseQuery;
import cn.rmc.basic.service.IBaseService;
import cn.rmc.basic.util.Pagelist;
import cn.rmc.org.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;


    @Override
    @Transactional
    public void add(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void upadte(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.remove(id);
    }

    @Override
    public T getById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> getAll() {
        return baseMapper.loadAll();
    }

    @Override
    public Pagelist<T> query(BaseQuery query) {
        long total = baseMapper.queryCount(query);
//        根据条件查询数据
        if (total==0) {
//            返回默认值
            return new Pagelist<>();
        }else {
            List<T> data =baseMapper.queryData(query);
            return new Pagelist<>(total,data);
        }
    }

    @Override
    public void delpatch(List<Long> ids) {
        baseMapper.delRemove(ids);
    }
}
